﻿#include "picshow.h"
#include "ui_picshow.h"


PicShow::PicShow(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::PicShow)
{
    ui->setupUi(this);
    ui->previousBtn->SetIcons(":/icon/previous.png",":/icon/previous_hover.png",":/icon/previous_press.png");
    ui->nextBtn->SetIcons(":/icon/next.png",":/icon/next_hover.png",":/icon/next_press.png");

    //实现将鼠标滑动到PicShow区域才显示前进和后退按钮，滑出PicShow区域则不显示。
    //为使按钮实现渐隐渐现的效果，所以我们通过动画实现。在PicShow的构造函数里创建两个渐隐渐现的动画,将效果绑定到两个按钮上。
    QGraphicsOpacityEffect * opacity_pre = new QGraphicsOpacityEffect(this);
    opacity_pre->setOpacity(0);//设置透明度0.5,透明范围：[0,1]
    ui->previousBtn->setGraphicsEffect(opacity_pre);

    QGraphicsOpacityEffect * opacity_next = new QGraphicsOpacityEffect(this);
    opacity_next->setOpacity(0);
    //应用到需要透明变化的控件；
    ui->nextBtn->setGraphicsEffect(opacity_next);

    //使用属性动画类让控件在透明度范围内变化
    _animation_show_pre = new QPropertyAnimation(opacity_pre,"opacity",this);
    //动画效果
    _animation_show_pre->setEasingCurve(QEasingCurve::Linear);
    //动画时长
    _animation_show_pre->setDuration(500);

    _animation_show_next = new QPropertyAnimation(opacity_next,"opacity",this);
    _animation_show_next->setEasingCurve(QEasingCurve::Linear);
    _animation_show_next->setDuration(500);


    //绑定按钮点击信号
    connect(ui->nextBtn,&QPushButton::clicked,this,&PicShow::SigNextClicked);
    connect(ui->previousBtn,&QPushButton::clicked,this,&PicShow::SigPreClicked);
}

PicShow::~PicShow()
{
    delete ui;
}


//实现重新加载逻辑
void PicShow::ReloadPic()
{
    if(_selected_path != "")
    {
        const auto &width = ui->gridLayout->geometry().width();
        const auto &height = ui->gridLayout->geometry().height();
        _pix_map.load(_selected_path);
        _pix_map = _pix_map.scaled(width,height,Qt::KeepAspectRatio);
        ui->label->setPixmap(_pix_map);
    }
}


//重写PicShow的event函数，捕获其中的enter和leave事件
bool PicShow::event(QEvent *event)
{
    switch(event->type())
    {
        case QEvent::Enter:
            ShowPreNextBtns(true);
            break;
        case QEvent::Leave:
            ShowPreNextBtns(false);
            break;
        default:
            break;
    }

    return QDialog::event(event);
}

void PicShow::ShowPreNextBtns(bool b_show)
{
    //需要隐藏状态并且当前是可见状态
    if(!b_show && _b_btnvisible)
    {
        _animation_show_pre->stop();
        _animation_show_pre->setStartValue(1);
        _animation_show_pre->setEndValue(0);
        _animation_show_pre->start();

        _animation_show_next->stop();
        _animation_show_next->setStartValue(1);
        _animation_show_next->setEndValue(0);
        _animation_show_next->start();

        _b_btnvisible = false;
        return;
    }

    //需要可见状态并且当前是隐藏状态
    if(b_show && !_b_btnvisible)
    {
        _animation_show_pre->stop();
        _animation_show_pre->setStartValue(0);
        _animation_show_pre->setEndValue(1);
        _animation_show_pre->start();

        _animation_show_next->stop();
        _animation_show_next->setStartValue(0);
        _animation_show_next->setEndValue(1);
        _animation_show_next->start();

        _b_btnvisible = true;
        return;
    }
}

//根据地址显示照片
void PicShow::SlotSelectItem(const QString &path)
{
    _selected_path = path;
    _pix_map.load(path);

    auto width = this->width() - 20;
    auto height = this->height() - 20;

    //KeepAspectRatio：该大小在给定矩形内被缩放为尽可能大的矩形，同时保留长宽比
    _pix_map = _pix_map.scaled(width,height,Qt::KeepAspectRatio);
    ui->label->setPixmap(_pix_map);
}

//更新图片
void PicShow::SlotUpdatePic(const QString &path)
{
    _selected_path = path;
    if(_selected_path != "")
    {
        const auto &width = ui->gridLayout->geometry().width();
        const auto &height = ui->gridLayout->geometry().height();
        _pix_map.load(_selected_path);
        _pix_map = _pix_map.scaled(width,height,Qt::KeepAspectRatio);
        ui->label->setPixmap(_pix_map);
    }
}

void PicShow::SlotDeleteItem()
{
    _selected_path = "";
}
